package cn.woblog.android.common.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.haixue.android.academy.utils.Consts;

/**
 * Created by ren on 2015/5/15 0015.
 */
public class SharePreferenceUtils {
    private static SharePreferenceUtils instance;
    private final SharedPreferences sharedPreferences;

    public SharePreferenceUtils(Context context) {
        sharedPreferences = context.getSharedPreferences(Const.CONFIG_FILENAME, Context.MODE_PRIVATE);
    }

    public static SharePreferenceUtils getInstance(Context context){
        if (instance==null){
            instance = new SharePreferenceUtils(context);
        }
        return instance;
    }

    public void putString(String key,String value){
        save(getEdit().putString(key, value));
    }
    public void putInt(String key,int value){
        save(getEdit().putInt(key, value));
    }
    public void putBoolean(String key,boolean value){
        save(getEdit().putBoolean(key, value));
    }
    public boolean getBoolean(String key,boolean defValue){
        return   sharedPreferences.getBoolean(key,defValue);
    }
    public int getInt(String key,int defValue){
        return   sharedPreferences.getInt(key, defValue);
    }
//    public int putInt(String key,int value){
//        return  putInt(key, value );
//    }
    private void save(SharedPreferences.Editor editor) {
        editor.apply();
    }

    private SharedPreferences.Editor getEdit() {
        return sharedPreferences.edit();
    }

    /**
     *
     * @return
     */
    public boolean isShowNavigation() {
        return getBoolean(Consts.KEY_IS_SHOW_NAVIGATION, true);
    }

    public void setNotShowNavigation() {
        putBoolean(Consts.KEY_IS_SHOW_NAVIGATION, false);
    }

    public String getSK() {
        return  sharedPreferences.getString(Consts.SK,"sk");
    }
    public void setSK(String sk) {
       putString(Consts.SK,sk);
    }

    public String[] getUserInfo() {
        String string = sharedPreferences.getString(Consts.KEY_USER_INFO, null);
        if (string!=null){
           return  string.split(",");
        }
        return  null;
    }

    public void setUserInfo(String info){
        putString(Consts.KEY_USER_INFO,info);
    }

    public int getSelectCategoryId() {
        return getInt(Consts.KEY_CATEGORY_ID,0);
    }
    public void setSelectCategoryId(int selectIndex) {
        putInt(Consts.KEY_CATEGORY_ID,selectIndex);
    }
}
